/*
 * 
 * 
 * 
 */
package com.hboxs.asl.template.directive;

import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.MemberService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
 * 模板指令 - 当前会员
 */
@Component("currentMemberDirective")
public class CurrentMemberDirective extends BaseDirective {

    /**
     * 变量名称
     */
    private static final String VARIABLE_NAME = "currentMember";

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    @SuppressWarnings("rawtypes")
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        Member currentMember = memberService.getCurrent();
        if (body != null) {
            setLocalVariable(VARIABLE_NAME, currentMember, env, body);
        } else {
            if (currentMember != null) {
                Writer out = env.getOut();
                out.write(currentMember.getUsername());
            }
        }
    }

}